//
//  AsyncTaskTrigger.m
//  AsyncTaskTrigger
//
//  Created by zhouzuosong on 2020/9/17.
//

#import "AsyncTaskTrigger.h"

static AsyncTaskTrigger *_trigger;

@interface AsyncTaskTrigger ()

@property (nonatomic, strong) NSMutableDictionary *groupMap;

@end

@implementation AsyncTaskTrigger

+ (instancetype)shareTrigger{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _trigger = [[AsyncTaskTrigger alloc] init];
    });
    return _trigger;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        _groupMap = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)addTaskAction:(LXTaskActionBlock)actionBlock asyncType:(LXTaskAsyncType)asyncType groupKey:(NSString *)groupKey completBlock:(LXTaskActionBlock)completBlock{
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    taskGroup.asyncType = asyncType;
    [taskGroup addActionBlock:actionBlock completBlock:completBlock];
}

- (void)addTaskAction:(LXTaskActionBlock)actionBlock asyncType:(LXTaskAsyncType)asyncType groupKey:(NSString *)groupKey {
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    taskGroup.asyncType = asyncType;
    [taskGroup addActionBlock:actionBlock];
}

- (void)addTaskAction:(LXTaskActionBlock)actionBlock groupKey:(NSString *)groupKey {
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    [taskGroup addActionBlock:actionBlock];
}

- (void)addTaskHandlerAction:(LXSemaphoreBlock)actionBlock
                   asyncType:(LXTaskAsyncType)asyncType
                    groupKey:(NSString *)groupKey
                completBlock:(LXTaskActionBlock)completBlock;{
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    taskGroup.asyncType = asyncType;
    [taskGroup addHandlerActionBlock:actionBlock completBlock:completBlock];
}

- (void)addTaskHandlerAction:(LXSemaphoreBlock)actionBlock asyncType:(LXTaskAsyncType)asyncType groupKey:(NSString *)groupKey {
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    taskGroup.asyncType = asyncType;
    [taskGroup addHandlerActionBlock:actionBlock];
}

- (void)addTaskHandlerAction:(LXSemaphoreBlock)actionBlock groupKey:(NSString *)groupKey {
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    [taskGroup addHandlerActionBlock:actionBlock];
}

- (void)prepareTaskGroupWithAsyncType:(LXTaskAsyncType)asyncType groupKey:(NSString *)groupKey {
    AsyncTaskItem *group = self.groupMap[groupKey];
    if (group) {
        NSLog(@"kael-async 已存在之前的taskItem %@",groupKey);
        // 如果 有正在工作中的队列 需要先暂停当前工作
        if ([group isWorking]) {
            [group suspendWork];
        }
        [group cancelOperation];
        [self.groupMap removeObjectForKey:group];
    }else{
        group = [[AsyncTaskItem alloc] init];
        group.taskGroupKey = groupKey;
        [self.groupMap setObject:group forKey:groupKey];
    }
}

- (void)cleanTaskGroupWithAsyncType:(LXTaskAsyncType)asyncType groupKey:(NSString *)groupKey {
    AsyncTaskItem *group = self.groupMap[groupKey];
    if (group) {
        // 如果 有正在工作中的队列 需要先暂停当前工作
        if ([group isWorking]) {
            [group suspendWork];
        }
        [self.groupMap removeObjectForKey:group];
    }
}

- (void)startTaskActionWithGroupKey:(NSString *)groupKey completBlock:(LXTaskActionBlock)completBlock{
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    [taskGroup startTaskActionWithCompletBlock:completBlock];
}

- (void)sendSemaphoreToGroupKey:(NSString *)groupKey{
    if (!groupKey || groupKey.length == 0) {
        return ;
    }
    AsyncTaskItem *taskGroup = [self groupWithKey:groupKey];
    [taskGroup sendSemaphore];
}

- (AsyncTaskItem *)groupWithKey:(NSString *)groupKey{
    
    AsyncTaskItem *group = self.groupMap[groupKey];
    if (!group) {
        group = [[AsyncTaskItem alloc] init];
        group.taskGroupKey = groupKey;
        [self.groupMap setObject:group forKey:groupKey];
    }
    return group;
}

@end
